package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.mall.enums.OrderInfoEnum;
import com.ruoyi.vote.admin.domain.VotePrizeReceive;
import com.ruoyi.vote.admin.mapper.VotePrizeMapper;
import com.ruoyi.vote.admin.mapper.VotePrizeReceiveMapper;
import com.ruoyi.vote.admin.notice.VoteNoticeJob;
import com.ruoyi.vote.admin.service.IVotePrizeReceiveService;
import com.ruoyi.vote.common.constants.ExamineConstants;
import com.ruoyi.weixin.domain.WxUser;
import com.ruoyi.weixin.service.WxUserService;
import liquibase.pro.packaged.W;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.admin.domain.bo.VotePrizeReceiveBo;
import com.ruoyi.vote.admin.domain.vo.VotePrizeReceiveVo;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 奖品申领Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-17
 */
@RequiredArgsConstructor
@Service
public class VotePrizeReceiveServiceImpl extends ServiceImpl<VotePrizeReceiveMapper,VotePrizeReceive> implements IVotePrizeReceiveService {

    private final VotePrizeReceiveMapper baseMapper;
    private final VotePrizeServiceImpl prizeService;
    private final WxUserService wxUserService;
    private final VoteNoticeJob noticeJob;

    /**
     * 查询奖品申领
     */
    @Override
    public VotePrizeReceiveVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询奖品申领列表
     */
    @Override
    public TableDataInfo<VotePrizeReceiveVo> queryPageList(VotePrizeReceiveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VotePrizeReceive> lqw = buildQueryWrapper(bo);
        Page<VotePrizeReceiveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.setRecords(result.getRecords().stream().peek(receive ->
            receive.setPrizeVo(prizeService.queryById(receive.getPrizeId()))
        ).collect(Collectors.toList()));
        return TableDataInfo.build(result);
    }

    /**
     * 查询奖品申领列表
     */
    @Override
    public List<VotePrizeReceiveVo> queryList(VotePrizeReceiveBo bo) {
        LambdaQueryWrapper<VotePrizeReceive> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VotePrizeReceive> buildQueryWrapper(VotePrizeReceiveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VotePrizeReceive> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAId() != null, VotePrizeReceive::getAId, bo.getAId());
        if (UserType.XCX_MEMBER.equals(LoginHelper.getUserType())){
            lqw.eq(VotePrizeReceive::getMId, LoginHelper.getUserId());
            lqw.orderByAsc(VotePrizeReceive::getStatus);
        }else{
            lqw.eq(bo.getMId() != null, VotePrizeReceive::getMId, bo.getMId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSn()), VotePrizeReceive::getOrderSn, bo.getOrderSn());
        lqw.like(StringUtils.isNotBlank(bo.getName()), VotePrizeReceive::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), VotePrizeReceive::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getSheng()), VotePrizeReceive::getSheng, bo.getSheng());
        lqw.eq(StringUtils.isNotBlank(bo.getShi()), VotePrizeReceive::getShi, bo.getShi());
        lqw.eq(StringUtils.isNotBlank(bo.getQu()), VotePrizeReceive::getQu, bo.getQu());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), VotePrizeReceive::getAddress, bo.getAddress());
        lqw.eq(bo.getStatus() != null, VotePrizeReceive::getStatus, bo.getStatus());
        lqw.orderByDesc(VotePrizeReceive::getCreateTime);
        return lqw;
    }

    /**
     * 新增奖品申领
     */
    @Override
    public Boolean insertByBo(VotePrizeReceiveBo bo) {
        VotePrizeReceive add = BeanUtil.toBean(bo, VotePrizeReceive.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改奖品申领
     */
    @Override
    public Boolean updateByBo(VotePrizeReceiveBo bo) {
        VotePrizeReceive update = BeanUtil.toBean(bo, VotePrizeReceive.class);
        if(UserType.XCX_MEMBER.equals(LoginHelper.getUserType())){
            update.setStatus(ExamineConstants.SUCCESS );
        }
        update.setFhStatus(OrderInfoEnum.STATUS_1.getValue());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除奖品申领
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 奖品发货给 / 订阅通知
     *
     * @param id 奖品表ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean prizeExamine(Long id) {
        VotePrizeReceiveVo votePrizeReceiveVo = queryById(id);
        if (ObjectUtil.isNotEmpty(votePrizeReceiveVo)){
            VotePrizeReceive votePrizeReceive = BeanUtil.toBean(votePrizeReceiveVo, VotePrizeReceive.class);
            votePrizeReceive.setFhStatus(OrderInfoEnum.STATUS_2.getValue());
            int update = baseMapper.updateById(votePrizeReceive);
            if (update > 0){
                WxUser wxUsers = wxUserService.getById(votePrizeReceive.getMId());
                noticeJob.shippingNotice(wxUsers,votePrizeReceiveVo);
                return true;
            }
        }
        return false;
    }
}
